﻿using System;
using System.Collections.Generic;
using System.Reflection;
using System.Text;

namespace Workflows.Components.TextMining.Collapse
{
    public class StringEvaluator:IFieldEvaluator
    {
        public FieldType EvaluableFieldType
        {
            get { return FieldType.Character; }
        }

        public double CalculateSimilarity(EvaluableFieldAttribute evalFieldAttr, object fieldValue1, object fieldValue2)
        {
            if(evalFieldAttr.FieldType !=this.EvaluableFieldType)
                throw new Exception("Can not evaluate field of this type");

            if(!evalFieldAttr.AllowNull)
            {
                if (fieldValue1 == null || fieldValue2 == null)
                    return 0;
            }
            else if(fieldValue1==null && fieldValue2==null)
            {
                return 1;
            }
            else if(fieldValue1==null || fieldValue2==null)
            {
                return 0;
            }

            double score = 0;
            string str1 = ((string) fieldValue1).ToLower().Trim();
            string str2 = ((string)fieldValue2).ToLower().Trim();
            switch (evalFieldAttr.EvaluateMethod)
            {
                case ValueEvaluateMethods.Contains:
                    if (str1.Contains(str2))
                        score = 1;
                    break;
                case ValueEvaluateMethods.Equals:
                    if (str1== str2)
                        score = 1;
                    break;
                case ValueEvaluateMethods.Exists:
                    score = 1;
                    break;
                case ValueEvaluateMethods.GE:
                    if (str1.CompareTo(str2) >= 0)
                        score = 1;
                    break;
                case ValueEvaluateMethods.GT:
                    if (str1.CompareTo(str2) > 0)
                        score = 1;
                    break;
                case ValueEvaluateMethods.IsA:
                    score = 1;
                    break;
                case ValueEvaluateMethods.LE:
                    if (str1.CompareTo(str2) <= 0)
                        score = 1;
                    break;
                case ValueEvaluateMethods.LT:
                    if (str1.CompareTo(str2) < 0)
                        score = 1;
                    break;
                case ValueEvaluateMethods.NE:
                    if (str1!=str2)
                        score = 1;
                    break;
                default:
                    break;
            }
            return score;
        }
    }
}
